Constructor | |
---|---|
Listbox ( master, **options ) | Constructor |
Options | |
---|---|
bg / fg | background / foreground. |
bd | border width. Default is 2 pixels. |
cursor | For ex: arrow , cross , hand etc. |
font | The font used for the text in the listbox. |
listvariable | A StringVar that is connected to the complete list of values in the listbox. |
selectmode |
|
height | Number of items visible in listbox. Default is 10. |
relif | Border Style. The default is SUNKEN. |
width | width in characters. The default is 20. |
Methods: | |
---|---|
insert ( index, *elements ) |
|
delete ( first, last=None ) | Deletes items from first to last or only single item at specified index. |
get ( first, last=None ) | Returns a tuple containing the list of all items |
curselection( ) |
|
activate ( index ) | Selects the line specifies by the given index. |
from tkinter import * from tkinter.messagebox import * class MyFrame(Tk): def __init__(self): super().__init__() self.lst=Listbox(self,height=5) self.lst.pack(padx=10,pady=10) self.lst.insert(0,"Amravati","Nagpur","Mumbai","Pune", "Nasik","Akola") self.btn=Button(self,text="Show",command=self.show) self.btn.pack(fill=X,padx=10,pady=10) def show(self): tp=self.lst.curselection() item=self.lst.get(tp[0]) showinfo(message=item) frm=MyFrame() frm.mainloop()
from tkinter import * class MyFrame(Tk): def __init__(self): super().__init__() self.data=StringVar() self.lst=Listbox(self,height=5) self.lst.pack(padx=10,pady=10) self.txt=Entry(self,textvariable=self.data) self.txt.pack(fill=X,padx=10,pady=10) self.btnx=Button(self,text="Add",command=self.add) self.btnx.pack(fill=X,padx=10,pady=10) self.btny=Button(self,text="Delete",command=self. delete) self.btny.pack(fill=X,padx=10,pady=10) def add(self): self.lst.insert(0,self.data.get()) self.data.set("") def delete(self): tp=self.lst.curselection() self.lst.delete(tp[0]) frm=MyFrame() frm.mainloop()